package com.dkts.oil.web.controller;

import com.alibaba.fastjson.JSONObject;
import com.dkts.oil.router.RouteStrategy;
import com.dkts.oil.router.RouteStrategyFactory;
import com.dkts.oil.router.impl.PipelineRouterInfoImpl;
import com.dkts.oil.router.param.PathInfo;
import com.dkts.oil.router.param.PipelineRouterInfo;
import com.dkts.oil.router.param.RouterInfo;
import com.dkts.oil.util.GeoJsonBuilder;
import com.dkts.oil.web.vo.request.PipelineRouteRequest;
import com.dkts.oil.web.vo.request.RouteCommonRequest;
import com.dkts.oil.web.vo.response.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/route")
@Tag(name = "路线规划相关功能")
@Slf4j
public class RouteController {

    @Autowired
    private RouteStrategyFactory strategyFactory;

    @Autowired
    private PipelineRouterInfoImpl routerInfo;

    /**
     *     YK_TO_ZY("YK_TO_ZY", "yk到zy站运输路线规划"),
     *     ZY_TO_CBK("ZY_TO_CBK", "zy站到cbk运输路线规划"),
     *     CBK_TO_XHK("CBK_TO_XHK", "cbk到xhk运输路线规划"),
     *     XHK_TO_WCGYZX("XHK_TO_WCGYZX", "xhk到wcgyzx运输路线规划"),
     *     LYC_TO_ZY("LYC_TO_ZY", "lyc到zy站运输路线规划"),
     *     YLCC("YLCC", "ylcc运输规划"),
     *     KQ_LAYOUT("KQ_LAYOUT", "kq内部布局与路由规划");
     * @param request
     * @return
     */
    @Operation(summary = "路线规划", description = "路线规划")
    @PostMapping
    public Result<RouterInfo> planRoute(@RequestBody RouteCommonRequest request) {
        long startTime = System.currentTimeMillis();
        log.info("planRoute {} request:{}", startTime, JSONObject.toJSONString(request));
        request.check();
        RouteStrategy strategy = strategyFactory.getStrategy(request.getRouteModel());
        Object result = strategy.planRoute(request);
        if (result instanceof RouterInfo) {
            String json = GeoJsonBuilder.buildGeoJsonFromPaths(((RouterInfo) result).getPaths());
            log.info("planRoute {} times:{} request:{}", startTime, System.currentTimeMillis() - startTime, json);
        }
        return Result.success(result);

    }


    @PostMapping("/pipeline")
    @Operation(summary = "库区内部管线规划", description = "库区内部管线规划")
    public Result<List<PipelineRouterInfo>> pipeline(@RequestBody PipelineRouteRequest request) throws Exception {
        long startTime = System.currentTimeMillis();
        log.info("pipeline {} request:{}", startTime, JSONObject.toJSONString(request));
        request.check();
        List<PipelineRouterInfo> list=routerInfo.pipelineRouter(request);
        log.info("pipeline {} times:{} request:{}", startTime, System.currentTimeMillis() - startTime, list);
        return Result.success(list);

    }
}
